package com.um.jdy.app.user.manager;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.internal.util.AlipayEncrypt;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.um.jdy.app.core.configuration.AlipayConfigure;
import com.um.jdy.common.commons.manager.WxManager;
import com.um.jdy.app.core.properties.HshAlipayProperties;
import com.um.jdy.app.core.properties.HshAppProperties;
import com.um.jdy.app.user.entity.dto.*;
import com.um.jdy.app.user.entity.vo.LoginVO;
import com.um.jdy.common.base.enums.YesOrNo;
import com.um.jdy.common.member.code.UserCode;
import com.um.jdy.common.member.entity.dto.AddressDTO;
import com.um.jdy.common.member.entity.po.Address;
import com.um.jdy.common.member.entity.po.LoginLog;
import com.um.jdy.common.member.entity.po.MemberUser;
import com.um.jdy.common.member.entity.vo.AddressVO;
import com.um.jdy.common.member.entity.vo.MemberUserVO;
import com.um.jdy.common.member.enums.MemberEnum;
import com.um.jdy.common.member.service.AddressService;
import com.um.jdy.common.member.service.MemberLoginLogService;
import com.um.jdy.common.member.service.MemberUserService;
import com.um.jdy.common.platform.service.AppService;
import com.um.jdy.common.system.service.UserService;
import com.um.springboot.starter.code.CommonCode;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.properties.HshProperties;
import com.um.springboot.starter.utils.IdUtils;
import com.um.springboot.starter.utils.JSONUtil;
import com.um.springboot.starter.utils.StringUtil;
import com.um.weixin.mini.service.WxMiniUserService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Title
 * @Description
 * @Author renjunwei
 * @Date 2023/5/22 10:37
 */
@Slf4j
@Service
public class AddressManager {

    @Autowired
    AddressService addressService;

    @Autowired
    MemberUserService userService;

   public List<AddressVO> getList(){
       List<Address> list = addressService.getList("member_user_id", userService.get().getMemberUserId(), "create_time", "desc");
       return BeanUtil.copyToList(list, AddressVO.class);
   }

   public AddressVO get(String id){
       String memberUserId = userService.get().getMemberUserId();
       Address address;
       if(StrUtil.isEmpty(id)){
           address = addressService.getEntity(new String[]{"member_user_id", "is_default"}, new String[]{memberUserId, YesOrNo.Yes.name()});
       }else{
           address = addressService.getEntity(new String[]{"member_address_id", "member_user_id"}, new String[]{id, memberUserId});
       }
       return BeanUtil.toBean(address, AddressVO.class);
   }

   public void add(AddressDTO dto){
       String memberUserId = userService.get().getMemberUserId();

       dto.setMemberUserId(memberUserId);
       dto.setCreateTime(DateUtil.date());
       if(StrUtil.equals(dto.getIsDefault(), YesOrNo.Yes.name())){
           Address defaultAddress = addressService.getEntity(new String[]{"member_user_id", "is_default"},
                   new String[]{memberUserId, YesOrNo.Yes.name()});
           if(null != defaultAddress){
               defaultAddress.setIsDefault(YesOrNo.No.name());
               addressService.updateEntity(defaultAddress);
           }
       }
       addressService.addEntity(BeanUtil.toBean(dto, Address.class));
   }

    public void update(String id, AddressDTO dto){
        String memberUserId = userService.get().getMemberUserId();
        Address address = addressService.getEntity(new String[]{"member_address_id", "member_user_id"}, new String[]{id, memberUserId});
        if(null == address){
            throw new ServiceException(UserCode.AddressNotExist);
        }

        Address entity = BeanUtil.toBean(dto, Address.class);
        entity.setMemberAddressId(id);
        entity.setMemberUserId(memberUserId);
        if(StrUtil.equals(dto.getIsDefault(), YesOrNo.Yes.name())){
            Address defaultAddress = addressService.getEntity(new String[]{"member_user_id", "is_default"},
                    new String[]{memberUserId, YesOrNo.Yes.name()});
            if(null != defaultAddress){
                defaultAddress.setIsDefault(YesOrNo.No.name());
                addressService.updateEntity(defaultAddress);
            }
        }
        addressService.updateEntity(entity);
    }

    public void delete(String id){
        String memberUserId = userService.get().getMemberUserId();
        Address address = addressService.getEntity(new String[]{"member_address_id", "member_user_id"}, new String[]{id, memberUserId});
        if(null == address){
            throw new ServiceException(UserCode.AddressNotExist);
        }

        addressService.delete(address);
    }
}
